package games.ninJump
{
	import actions.PropsAction;
	
	import core.Global;
	
	import events.Msg_GameOver;
	import events.Msg_GamePause;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	
	import games.ninJump.data.GameState;
	import games.ninJump.data.NinjaState;
	import games.ninJump.data.TimeUpdate;
	import games.ninJump.ui.ScoreShow;
	import games.ninJump.ui.actions.Ninja;
	import games.ninJump.ui.effects.MakeEnemies;
	import games.ninJump.ui.effects.NinjaParticles;
	import games.ninJump.ui.effects.WallMove;
	
	import interfaces.IGame;
	import interfaces.IMsgListener;
	
	public class NinJump implements IGame,IMsgListener
	{
		private static var MSG_TABLE:Array = [
			Msg_GameOver.M_GAMEOVER,
		];
		public static var GAME_URL:String = "ui/NinJumpDeluxeHD.swf";
		public var m_info:NinjaState;
		public var m_gameState:GameState;
		public var m_updater:TimeUpdate;
		
		private var m_menu:Sprite;
		private var m_deathMenu:Sprite;
		private var m_options:Sprite;
		private var m_game:Sprite;
		private var m_bg:Sprite;
		private var m_wall:Sprite;
		private var m_scrollWall:WallMove;
		private var m_fg:Sprite;
		private var m_score:Sprite;
		private var m_scoreShow:ScoreShow;
		
		private var m_ninja:Ninja;
		private var m_ninpaticle:NinjaParticles;
		
		//private var m_enemies:MakeEnemies;
		
		public function NinJump()
		{
		}
		
		public function msgManager(e:* = null):void
		{
			switch( e.data.msgid)
			{
				case Msg_GameOver.M_GAMEOVER:
					onGameOver(e.data);
					break;
				 
			}
		}
		
		public function start():void
		{
			init();
		}
		
		public function destroy():void
		{
		}
		
		// ----------------------------------------------------------------

	
		
		private function init():void
		{
			trace("init nin jump...");
			initData();
			
			initGameUi();
			initMenu();
			initDeathMenu();
			initOptions();
			
			initListener();
		 
			
		}
		
		private function initData():void
		{
			m_gameState = new GameState(this);
			m_info = new NinjaState();
			m_updater = new TimeUpdate();
		
		}
			
		
		private function initGameUi():void
		{
			m_game = new Sprite();
			Global.stage.addChild(m_game);
			
			m_bg = new Sprite();
			m_game.addChild(m_bg);
			m_bg.addChild(getBitmapByName("background"));
			
			
			
			m_wall = new Sprite();
			m_game.addChild(m_wall);
			
			m_ninja = new Ninja(m_info);
			m_game.addChild(m_ninja);
			
			m_fg = new Sprite();
			m_game.addChild(m_fg);
			m_fg.addChild(getBitmapByName("foreground"));
			
			//createWall();
			//var wall:Sprite = createWall();
			//m_wall.addChild(wall);
			//m_scrollWall = new ScrollWall();
			
			m_scrollWall = new WallMove(m_gameState);
			m_scrollWall.bmd = getBitmapDataByName("wall");
			m_wall.addChild(m_scrollWall);
			
			//m_enemies = new MakeEnemies(m_gameState);
			//m_wall.addChild(m_enemies);
			
			m_score = new Sprite();
			m_game.addChild(m_score);
			var scoreBg:Bitmap = getBitmapByName("score_bar");
			scoreBg.x = (GameState.GAME_WIDTH - scoreBg.width) / 2;
			scoreBg.y = 2;
			m_score.addChild(scoreBg);
			
			m_scoreShow = new ScoreShow(getBitmapDataByName("digits"));
			m_score.addChild(m_scoreShow);
			m_scoreShow.data = m_gameState;
			//m_scoreShow.showScore(521);
			
			var pause_button:SimpleButton = new SimpleButton();
			m_game.addChild(pause_button);
			pause_button.downState = getBitmapByName("pause_button");
			pause_button.upState = getBitmapByName("pause_button");
			pause_button.overState = getBitmapByName("pause_button");
			pause_button.hitTestState =  getBitmapByName("pause_button");
			pause_button.x = GameState.GAME_WIDTH - pause_button.width ;
			pause_button.y = GameState.GAME_HEIGHT - pause_button.height;
			pause_button.addEventListener(MouseEvent.CLICK,onClickPause);
		
		}
		
		private function initMenu():void
		{
			m_menu = new Sprite();
			Global.stage.addChild(m_menu);
			
			var main:Bitmap = getBitmapByName("main_castle");
			m_menu.addChild(main);
			
			var castle_button:SimpleButton = new SimpleButton();
			castle_button.downState = castle_button.upState = castle_button.overState = castle_button.hitTestState = getBitmapByName("castle_button");
			m_menu.addChild(castle_button);
			castle_button.x = GameState.GAME_WIDTH - castle_button.width;
			castle_button.y = GameState.GAME_HEIGHT - 200;
			castle_button.addEventListener(MouseEvent.CLICK,onClickCastle);
			
			var options_button:SimpleButton = new SimpleButton();
			m_menu.addChild(options_button);
			options_button.downState = getBitmapByName("options_button");
			options_button.upState = getBitmapByName("options_button");
			options_button.overState = getBitmapByName("options_button");
			options_button.hitTestState =  getBitmapByName("options_button");
			options_button.x = GameState.GAME_WIDTH - castle_button.width;
			options_button.y =  GameState.GAME_HEIGHT - 150;
			options_button.addEventListener(MouseEvent.CLICK,onClickOptions);
			
			
			
		}
		
		private function initDeathMenu():void
		{
			m_deathMenu = new Sprite();
			Global.stage.addChild(m_deathMenu);
			
			var menu:Bitmap = getBitmapByName("death_menu");
			menu.x = (GameState.GAME_WIDTH - menu.width)/2;
			menu.y = (GameState.GAME_HEIGHT - menu.height)/2;
			m_deathMenu.addChild(menu);
			
			var again_button:SimpleButton = new SimpleButton();
			again_button.downState = again_button.upState = again_button.overState = again_button.hitTestState = getBitmapByName("replay_button");
			m_deathMenu.addChild(again_button);
			again_button.x = (GameState.GAME_WIDTH - again_button.width)/2 - 50;
			again_button.y = (GameState.GAME_HEIGHT - again_button.height)/2 + 100;
			again_button.addEventListener(MouseEvent.CLICK,onClickAgain);
			m_deathMenu.visible = false; 
		}
		
		private function initOptions():void
		{
			m_options = new Sprite();
			var options_castle:Bitmap = getBitmapByName("options_castle");
			m_options.addChild(options_castle);
			Global.stage.addChild(m_options);
			m_options.visible = false;
			
			var back_button:SimpleButton= new SimpleButton();
			back_button.overState = back_button.downState = back_button.upState = back_button.hitTestState = getBitmapByName("back_button");
			m_options.addChild(back_button);
			back_button.y = 400;
			back_button.addEventListener(MouseEvent.CLICK,onClickBack);
		}
		
		private function initNin():void
		{
			// 忍者粒子效果	
			
			/*m_ninpaticle = new NinjaParticles();
			m_ninpaticle.state = 1;
			m_ninpaticle.follow = m_ninja.target;
			Global.stage.addChild(m_ninpaticle.target);
*/
			
			// 爆炸
			/*
			var fireball:Sprite = createExplodeParticles("fireball1",200,200);
			Global.stage.addChild(fireball);
			
			var smoke:Sprite = createExplodeParticles("smoke",200,300);
			Global.stage.addChild(smoke);
			
			var shield:Sprite = createExplodeParticles("shield",200,400);
			Global.stage.addChild(shield);
			*/
		
		}
		
		private function initListener():void
		{
			Global.addListener(Msg_GameOver.M_GAMEOVER,this);
			Global.stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
		}
		
		private function startGame():void
		{
			Global.clearAllAction();
			while(GameState.ENEMIES && GameState.ENEMIES.length > 0)
				(GameState.ENEMIES.shift()).destroy();
			
			
			m_gameState.init();
			
			m_info.state = NinjaState.NINJA_CLIMBING;
			
			Global.addAction(m_gameState);
			Global.addAction(m_updater);
			
			m_updater.registerObj(m_scrollWall);
			m_updater.registerObj(m_ninja);
		//	m_updater.registerObj(m_enemies);
			
			m_ninja.init();
			m_fg.y = 0;
			
			
			moveFg();
		}
		//--------------------------------------界面------------------------------
		public function get bg():Sprite
		{
			return m_bg;
		}
		
		public function get fg():Sprite
		{
			return m_fg;
		}
		
		public function get wall():Sprite
		{
			return m_wall;
		}
		
		// -------------------------------- 监听器------------------------------------\\
		private function onClickOptions(e:MouseEvent):void
		{
			m_options.visible = true;
		}
		
		private function onClickBack(e:MouseEvent):void
		{
			m_options.visible = false;
		}
		
		private function onClickCastle(e:MouseEvent):void
		{
			m_menu.visible = false;
			startGame();
		}
		
		private function onClickAgain(e:MouseEvent):void
		{
			m_deathMenu.visible = false;
			startGame();
		}
		
		private function onClickPause(e:MouseEvent):void
		{
			Global.dispatchEvent(Msg_GamePause.M_GAMEPAUSE,Msg_GamePause.createMessage());
		}
		
		private function onKeyDown(e:KeyboardEvent):void
		{
			if(m_info.state == NinjaState.NINJA_CLIMBING)
			{
				m_info.state = NinjaState.NINJA_JUMPING;
			}

		}
		
		private function onGameOver(e:Msg_GameOver):void
		{
			Global.deleteAction(m_gameState);
			Global.deleteAction(m_updater);
			m_deathMenu.visible = true;
		}
		
		//------------------------------/ 监听器----------------------------------\\
		// ----------------------------公用方法------------------------------------\\
		
		public function getBitmapByName(name:String):Bitmap
		{
			//var atom:* = Global.loadResource(GAME_URL,name);
			//var bmd:BitmapData = atom.obj;
			var bm:Bitmap = new Bitmap(getBitmapDataByName(name));
			return bm;
		}
		
		public function getBitmapDataByName(name:String):BitmapData
		{
			var atom:* = Global.loadResource(GAME_URL,name);
			var bmd:BitmapData = atom.obj;
		
			return bmd;
		}
		
		private function createExplodeParticles(name:String,x:Number = 0,y:Number = 0):Sprite
		{
			var container:Sprite = new Sprite();
			var sp:Sprite ;
			var atom:* = Global.loadResource(NinJump.GAME_URL,name);
			var bmd:BitmapData = atom.obj;
			var bm:Bitmap;
			for(var i:int = 0; i < 100; i++)
			{
				bm = new Bitmap(bmd);
				
				bm.x = - bm.width / 2;
				bm.y = - bm.height / 2;
				bm.smoothing = true;
				//trace(bm.x,bm.y,bm.width,bm.height);
				sp = new Sprite();
				sp.addChild(bm);
				container.addChild(sp);
			}
			//Global.stage.addChild(container);
			container.x = x;
			container.y = y;
			
			//var ef:ExplodeEffect = new ExplodeEffect(container);
			return container;
		}
		
		
		
		private function moveFg():void
		{
			var pa:PropsAction = new PropsAction(m_fg);
			pa.props = [{prop:"y",init:0,end: GameState.GAME_HEIGHT }];
			pa.speed = m_gameState.speed * .0025;
			pa.acceleration = 0;
			Global.addAction(pa);
			
		}
		// ----------------------------/公用方法------------------------------------\\
	}
}